from CvPythonExtensions import *

import GerikSpells
from GerikSpells import GerikSpellBase
from GSGroupPolicies import GSAll as GSGroup
from GSHandlerPolicies import GSImmediateNoTarget as GSHandler
from GSResultsPolicies import GSMissionNoTarget as GSResults

import GerikSpellsUtils

gc = CyGlobalContext()
localtext = CyTranslator()

class SiegeMode(GerikSpellBase, GSGroup, GSHandler, GSResults):
    def __init__(self):
        GerikSpellBase.__init__(self)
        GSGroup.__init__(self)
        GSHandler.__init__(self)
        GSResults.__init__(self)

        self.m_iSiegeResearchNeeded = gc.getInfoTypeForString("PROJECT_TERRAN_SIEGE_TANK_SIEGE_MODE")
        self.m_iSiegeResearchBuildingBuiltAt = gc.getProjectInfo(self.m_iSiegeResearchNeeded).getBuildingToBuildAt()
        self.m_szNotValidText = localtext.getText( "TXT_KEY_CIVCRAFT_REQUIRES_RESEARCH_AT", (gc.getProjectInfo(self.m_iSiegeResearchNeeded).getDescription(),
                                                                                             gc.getBuildingInfo(self.m_iSiegeResearchBuildingBuiltAt).getDescription()) )


    def getHelpText(self, pGroup, pPlot):
        bValid = self.canGroupCast(pGroup, pPlot, False)
        if not bValid:
            return self.m_szNotValidText
            
    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        if not gc.getUnitInfo(pCastingUnit.getUnitType()).canSiegeMode():
            return False

        # Don't show the "siege mode" button for units already in siege mode.
        if pCastingUnit.isInSiegeMode():
            return False

        if bVisible:
            return True

        # Check if they have the research.
        if not gc.getPlayer( pCastingUnit.getOwner() ).hasBuiltProject( self.m_iSiegeResearchNeeded ):
            return False

        return True
        
    def startSpellResults(self, pCastingGroup, pTarget):
        for pUnit in pCastingGroup.getUnits():
            pUnit.setSiegeMode(True)
            pUnit.finishMoves()

        # Don't continue the mission.
        return False

class TankMode(GerikSpellBase, GSGroup, GSHandler, GSResults):
    def __init__(self):
        GerikSpellBase.__init__(self)
        GSGroup.__init__(self)
        GSHandler.__init__(self)
        GSResults.__init__(self)

    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        if not pCastingUnit.isInSiegeMode():
            return False

        return True
        
    def startSpellResults(self, pCastingGroup, pTarget):
        for pUnit in pCastingGroup.getUnits():
            pUnit.setSiegeMode(False)
            pUnit.finishMoves()

        # Don't continue the mission
        return False


        

                        
            
        
            
            
